﻿using System;
using System.Data;
using System.Configuration;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using RuleCode.Common;
using System.Reflection;

namespace RuleCode.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:RuleButton runat=server></{0}:RuleButton>")]
    public class RuleButton : Button
    {
        #region 声明要显示的控件
        public Button fieldButton; 
        #endregion

        #region 构造方法
        public RuleButton()
            : base()
        {
            
        }
        #endregion
        #region 显示方法
        //撰写
        protected override void CreateChildControls()
        {
            this.fieldButton = new Button();

            load();

            Controls.Clear();
            this.Controls.Add(this.fieldButton);
            ChildControlsCreated = true;
        }
        //布局
       
        protected override void Render(HtmlTextWriter writer)
        {
            EnsureChildControls();

            AddAttributesToRender(writer);


            fieldButton.RenderControl(writer);

      
        }
        #endregion
        #region 外部方法

        public string PermissionID
        {
            get
            {
                if (ViewState["PermissionID"] != null)
                {
                    return ViewState["PermissionID"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["PermissionID"] = value;
            }
        }
        public string Text
        {
            get
            {
                if (ViewState["Text"] != null)
                {
                    return ViewState["Text"].ToString();
                }
                return "";
            }
            set
            {
                ViewState["Text"] = value;
            }
        }
        
        #endregion

        #region 内部方法
        private void load()
        {
            fieldButton.Text = Text;
            fieldButton.Click += OnButtonClick;
        }

        public delegate void ButtonClickEventHandler(object sender, EventArgs e); 
        public event ButtonClickEventHandler ButtonClick;
        protected virtual void OnButtonClick(object sender, EventArgs e)
        { 
            if (ButtonClick != null)
            {
                ButtonClick(this, e); 
            }
        }
        #endregion
    }
}
